<?php

class controller_application extends controller_theme
{
	public function __construct()
	{
		parent::__construct();
	}
	

	public function listing()
	{
		$model = bone::get_model('application', APP_DIR);
		$applications = $model->get_list();

		$template = bone::get_template('application.listing', APP_DIR);
		$template->set_title('已安装的程序');
		$template->set('applications', $applications);
		$template->set_data($this->get_data());
		$template->display();
	}

	
	public function online_listing()
	{
		$key = get('key', null);
		$template = bone::get_template('application.online_listing', APP_DIR);

		$config = bone::get_config('bone_webservices', APP_DIR);
		$soap = new SoapClient(null, array('location'=>$config->uri_application, 'uri'=>$config->uri));

		$limit = 8;
		$pagination = $template->get_pagination();
		$pagination->set_limit( $limit );
		$pagination->set_page(get('page', 1, 'int'));
		if($key)
		{
			$pagination->set_total( $soap->get_search_total($key) );
			$pagination->set_url('./?controller=application&task=online_listing&key='.$key);
			$application_list = $soap->get_search_list($key, $pagination->get_offset(), $limit);
		}
		else
		{
			$pagination->set_total( $soap->get_total() );
			$pagination->set_url('./?controller=application&task=online_listing');
			$application_list = $soap->get_list($pagination->get_offset(), $limit);
		}

		$model = bone::get_model('application', APP_DIR);
		$installed_application_list = $model->get_list();
		$installed_application = array();
		foreach($installed_application_list as $application)
		{
			$installed_application[] = $application->application_id;
		}

		$template->set_title('安装新应用');
		$template->set('application_list', $application_list);
		$template->set('installed_application', $installed_application);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function ajax_install()
	{
		$application_id = get('application_id', 0, 'int');
		if($application_id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{application_id}缺失');
			$this->ajax();
		}

		$config = bone::get_config('bone_webservices', APP_DIR);
		$soap = new SoapClient(null, array('location'=>$config->uri_application, 'uri'=>$config->uri));
		$application = $soap->get($application_id);

		if(!$application)
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', '没有找到指定的应用');
			$this->ajax();
		}

		// 下载相关文件
		$package =  $application_id.'.zip';
		$thumbnail = $application_id.'.'.array_pop(explode('.',$application->thumbnail ));
		$image = $application_id.'.'.array_pop(explode('.',$application->image ));

		$path = BONE_ROOT.DS.APP_DIR.DS.'storage'.DS.'applications';
		$package_path =  $path.DS.'packages'.DS.$package;
		$thumbnail_path = $path.DS.'thumbnails'.DS.$thumbnail;
		$image_path = $path.DS.'images'.DS.$image;

		copy('http://www.mrbone.org/./?controller=client_application&task=download&application_id='.$application_id, $package_path);
		copy( 'http://www.mrbone.org/'.$application->thumbnail, $thumbnail_path );
		copy( 'http://www.mrbone.org/'.$application->image, $image_path );

		$tmp_dir = BONE_ROOT.DS.APP_DIR.DS.'tmp'.DS.'applications_'.$application_id;

		// 解压到临时文件夹
		$zip = bone::get('zip');
		$zip->open($package_path);
		if(!$zip->extract_to($tmp_dir))
		{
			$this->set_ajax('status', 3);
			$this->set_ajax('description', $zip->get_error());
			$this->ajax();
		}


		// 检查是否与当前系统冲容
		$model = bone::get_model('application', APP_DIR);
		if(!$model->validate($application_id, $application->application))
		{
			$this->set_ajax('status', 4);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}

		// 复制文件
		if(!$model->install($application_id, $application->application, $tmp_dir))
		{
			$this->set_ajax('status', 5);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}

		// 删除临时文件
		$fso = bone::get('fso');
		$fso->rm_dir($tmp_dir);

		// 写入数据库
		$table = bone::get_table('application', APP_DIR);
		$table->bind($application);
		$table->id = 0;
		$table->application_id = $application_id;
		$table->thumbnail = 'storage/applications/thumbnails/'.$thumbnail;
		$table->image = 'storage/applications/images/'.$image;
		$table->install_time = time();
		$table->save();

		if( count($application->frontend_controllers) )
		{
			foreach($application->frontend_controllers as $controller)
			{
				$table = bone::get_table('application_frontend_controller', APP_DIR);
				$table->bind($controller);
				$table->id = 0;
				$table->application_id = $application_id;
				$table->save();
			}
		}

		if( count($application->backend_controllers) )
		{
			foreach($application->backend_controllers as $controller)
			{
				$table = bone::get_table('application_backend_controller', APP_DIR);
				$table->bind($controller);
				$table->id = 0;
				$table->application_id = $application_id;
				$table->save();
			}
		}


		// 安装成功
		$this->set_ajax('status', 0);
		$this->set_ajax('description', '安装成功');
		$this->ajax();
	}


	public function ajax_delete()
	{
		$id = get('id', 0, 'int');
		if($id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{id}缺失');
			$this->ajax();
		}

		if($id<=1024)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '系统自带的程序不能删除');
			$this->ajax();
		}

		$model = bone::get_model('application', APP_DIR);
		if( $model->uninstall($id) )
		{
			$this->set_ajax('status', 0);
			$this->set_ajax('description', '删除成功');
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}
	}


	
}
?>